/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.dafe.partyband.services.impl.songsterr;

import java.net.URI;
import java.text.Normalizer;
import java.util.ArrayList;
import java.util.List;
import org.dafe.partyband.db.api.IAuthor;
import org.dafe.partyband.db.api.ISong;
import org.dafe.partyband.services.api.IMusicService;
import org.dafe.partyband.services.api.IURIActionDescription;
import org.dafe.partyband.services.impl.SimpleURIBuilder;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;

@ServiceProvider(service=IMusicService.class)
public class SongsterrService implements IMusicService {
    
    private static final String BEST_MATCH_URL_BASE = "http://www.songsterr.com/a/wa/bestMatchForQueryString";

    @Override
    public String getDisplayName() {
        return NbBundle.getMessage(SongsterrService.class, "LBL_Songsterr");
    }

    @Override
    public String getDisplayHint() {
        return NbBundle.getMessage(SongsterrService.class, "HINT_Songsterr");
    }

    @Override
    public List<IURIActionDescription<ISong>> getSongURLActionDescriptions() {
        final IURIActionDescription<ISong> songTab = new IURIActionDescription<ISong>() {
            @Override
            public String getName() {
                return NbBundle.getMessage(SongsterrService.class, "LBL_SongTab");
            }

            @Override
            public URI getURI(ISong song) {
                SimpleURIBuilder urlbuilder = new SimpleURIBuilder(BEST_MATCH_URL_BASE);
                urlbuilder.addQuery("s", removeDiacritics(song.getName()));
                urlbuilder.addQuery("a", removeDiacritics(song.getAuthor().getName()));
                
                return urlbuilder.buildURI();
            }
        };
        
        return new ArrayList<IURIActionDescription<ISong>>() {{ add(songTab); }};
    }

    @Override
    public List<IURIActionDescription<IAuthor>> getAuthorURLActionDescriptions() {
        final IURIActionDescription<IAuthor> artistTabs = new IURIActionDescription<IAuthor>() {
            @Override
            public String getName() {
                return NbBundle.getMessage(SongsterrService.class, "LBL_ArtistTabs");
            }

            @Override
            public URI getURI(IAuthor artist) {
                SimpleURIBuilder urlbuilder = new SimpleURIBuilder(BEST_MATCH_URL_BASE);
                urlbuilder.addQuery("s", "neco co neexistuje proste totalni kravina");
                urlbuilder.addQuery("a", removeDiacritics(artist.getName()));
                
                return urlbuilder.buildURI();
            }
        };
        
        return new ArrayList<IURIActionDescription<IAuthor>>() {{ add(artistTabs); }};
    }
    
    private String removeDiacritics(String name) {
        String trimmedName = name.trim();
        String decomposed = Normalizer.normalize(trimmedName, Normalizer.Form.NFD);
        return decomposed.replaceAll("[^\\p{ASCII}]", "");
    }
    
}
